package com.zhq.wx.controller;

import com.zhq.bean.Courier;
import com.zhq.bean.Message;
import com.zhq.bean.User;
import com.zhq.mvc.ResponseBody;
import com.zhq.service.CourierService;
import com.zhq.service.UserService;
import com.zhq.util.*;

import javax.lang.model.element.NestingKind;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * @Author:钟豪强
 * @Description:    微信端登录
 * @Date Created in  2020-08-19 12:49
 * @Modified By:
 */
public class UserController {


    @ResponseBody("/wx/loginSms.do")
    public String sendSms(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        //String code = RandomUtil.get()+"";
        // TODO:因为没钱,没发短信
        //boolean flag = SMSUtil.loginSMS(userPhone, code);
         String code = "123456";
         boolean flag = true;
        Message msg = new Message();
        if (flag) {
            msg.setStatus(0);
            msg.setResult("验证码已发送,请查收");
        }else{
            msg.setStatus(-1);
            msg.setResult("验证码发送失败,请检查手机号或稍后再试");
        }
        //把短信验证码和电话存在session里面
        // TODO:因为没钱,没发短信,code设置为123456
        UserUtil.setLoginSms(request.getSession(),userPhone,code);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String userPhone = request.getParameter("userPhone");
        String userCode = request.getParameter("code");
        String sysCode = UserUtil.getLoginSms(request.getSession(), userPhone);
        Message msg = new Message();
        if (sysCode == null) {
            //这个手机号未获取短信
            msg.setStatus(-1);
            msg.setResult("手机号码未获取短信");
        }else if (sysCode.equals(userCode)){
            //手机号码和短信一致,登录成功
            //判断快递员登录还是用户登录,快递员权限比用户大
            Courier c = CourierService.findByUserPhone(userPhone);
            User u = UserService.findByPhone(userPhone);
            User user = new User();
            user.setUserPhone(userPhone);
            System.out.println(userPhone);
            System.out.println(user);
            UserUtil.setWxUser(request.getSession(),user);
            if (c != null) {
                //快递员
                msg.setStatus(1);
                user.setUser(false);
            }else if ( u != null){
                //普通用户
                msg.setStatus(0);
                user.setUser(true);
            }else{
                //表示用户不存在,既不是用户也不是快递员
                //自动注册
                boolean flag = UserService.insert(userPhone, userPhone, null, null);
                if (flag) {
                    //注册成功
                    msg.setStatus(2);
                    msg.setResult("注册成功");
                }else{
                    //注册失败
                    msg.setStatus(-3);
                    msg.setResult("注册错误,请重新注册");
                }
            }
        }else{
            //验证码不一致,登录失败
            msg.setStatus(-2);
            msg.setResult("验证码不一致,请检查");
        }
        // TODO:
        System.out.println(msg.toString());

        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/wx/userInfo.do")
    public String userInfo(HttpServletRequest request, HttpServletResponse response){
        User user = UserUtil.getWxUser(request.getSession());
        boolean isUser = user.isUser();
        Message msg = new Message();
        if (isUser) {
            //表明是用户
            msg.setStatus(0);
        }else{
            //表明是快递员
            msg.setStatus(1);
        }
        msg.setResult(user.getUserPhone());
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/wx/logout.do")
    public String logout(HttpServletRequest request, HttpServletResponse response){
        //1.销毁session
        User user = UserUtil.getWxUser(request.getSession());
        System.out.println(user);
        Timestamp date = new Timestamp(System.currentTimeMillis());

        if (user.isUser()) {
            User u = UserService.findByPhone(user.getUserPhone());
            UserService.updateLastLoginTime(u.getId(), date);
        }else{
            Courier c = CourierService.findByUserPhone(user.getUserPhone());
            CourierService.updateLastLoginTime(c.getId(),date);
        }
        System.out.println(user);
        request.getSession().invalidate();
        //2.给客户端回复消息
        Message msg = new Message(0);
        return JSONUtil.toJSON(msg);
    }

    @ResponseBody("/user/lazyboard.do")
    public String lazyBoard(HttpServletRequest request, HttpServletResponse response){
        int type = Integer.valueOf(request.getParameter("type"));
        List<User> lazyboard = UserService.lazyboard(type);
        Message msg = new Message();
        if (lazyboard != null) {
            msg.setStatus(0);
            msg.setData(lazyboard);
        }else{
            msg.setStatus(-1);
            msg.setResult("查询失败");
        }
        return JSONUtil.toJSON(msg);
    }
}
